package com.wkq.jdbctemplate.commons;


import javax.servlet.http.HttpSession;
import java.io.Serializable;
import java.util.Date;

/**
 * Created by yuj-b on 2017/1/10.
 */
public class SessionCtx {

    // TODO 暂时解决物联网 tid 问题
    private static ThreadLocal<Long> localTenantId = new InheritableThreadLocal<>() ;

    private static ThreadLocal<HttpSession> localSession = new InheritableThreadLocal<>() ;

    public static long tid(){
        if (getSession()==null) {
            return localTenantId.get();
        } else {
            return getUser().getTenantId();
        }
    }

    public static void setTenantId(Long id){
        localTenantId.set(id);
    }

    public static User getUser() {
        return (User)getSession().getAttribute("session-user");
    }

    public static void setUser(User user) {
        getSession().setAttribute("session-user", user);
    }

    public static void setTenantVersion(String version) {
        getSession().setAttribute("tenantVersion", version);
    }

    public static String getTenantVersion(){
        return (String) getSession().getAttribute("tenantVersion");
    }

    public static HttpSession getSession() {
        return localSession.get();
    }

    public static void setSession(HttpSession session) {
        localSession.set(session);
    }

    public static class User implements Serializable {

        private static final long serialVersionUID = 761947661718279025L;
        /**
         *
         * @param id
         * @param name
         * @param role
         * @param tenantId
         * @param loginTime
         */
        public User(Long id, String name, String role, Long tenantId, Date loginTime) {
            this.id = id;
            this.name = name;
            this.role = role;
            this.tenantId = tenantId;
            this.loginTime = loginTime;
        }

        /**
         * 客户端User，带当前项目ID
         * @param id
         * @param name
         * @param projectId
         * @param tenantId
         * @param loginTime
         */
        public User(Long id, String name, String role, Long projectId, Long tenantId, Date loginTime) {
            this.id = id;
            this.name = name;
            this.role = role;
            this.projectId = projectId;
            this.tenantId = tenantId;
            this.loginTime = loginTime;
        }


        /**
         * 云端User
         * @param id
         * @param name
         * @param tenantId
         * @param loginTime
         * @param tenantType 租户类型：1房建，2基建
         */
        public User(Long id, String name, String role, Long tenantId,int tenantType, Date loginTime) {
            this.id = id;
            this.name = name;
            this.role = role;
            this.tenantId = tenantId;
            this.loginTime = loginTime;
            this.tenantType = tenantType;
        }

        /**
         * 云端User 带项目ID
         * @param id
         * @param name
         * @param tenantId
         * @param tenantType 租户类型：1房建，2基建
         * @param projectId
         * @param loginTime
         */
        public User(Long id, String name, String role, Long tenantId,int tenantType,Long projectId, Date loginTime) {
            this.id = id;
            this.name = name;
            this.role = role;
            this.tenantId = tenantId;
            this.tenantType = tenantType;
            this.loginTime = loginTime;
            this.projectId = projectId;
        }


        private Long id ;
        private String name ;
        private String role ;
        private Long tenantId ;
        private Date loginTime ;
        private int tenantType;

        public Date getLoginTime() {
            return loginTime;
        }

        public void setLoginTime(Date loginTime) {
            this.loginTime = loginTime;
        }

        public Long getId() {
            return id;
        }

        public void setId(Long id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getRole() {
            return role;
        }

        public void setRole(String role) {
            this.role = role;
        }

        public Long getTenantId() {
            return tenantId;
        }

        public void setTenantId(Long tenantId) {
            this.tenantId = tenantId;
        }

        public int getTenantType() {
            return tenantType;
        }

        public void setTenantType(int tenantType) {
            this.tenantType = tenantType;
        }

        /**
         * 客户端登录用户当前项目Id
         */
        private Long projectId ;

        public Long getProjectId() {
            return projectId;
        }

        public void setProjectId(Long projectId) {
            this.projectId = projectId;
        }

        public boolean isAdmin(){
            return "admin".equals(role);
        }
    }

}
